package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.po.TIaimtostudent;
import com.zmht.beike.po.TImonthcontent;
import com.zmht.beike.po.TImonthinfo;
import com.zmht.specedu.vo.Teacher;
/**
 * 个别化目标基本信息
 * @author Administrator
 *
 */
public class IAiminfoAction extends BaseAction {
	/**
	 * 初始化个别教学目标添加页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initIAiminfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//获得集体目标对应的学生
		//String sql = "select s.StudentId,s.SName,s.ClassID from classteacher ct,student s where s.ClassID=ct.ClassID and TeacherID = "+teacher.getTeacherId();
		String sql = "select ic.IClassID,ic.Name from individualclass ic where ic.TeacherID="+teacher.getTeacherId();
		
		List list = this.getCareaContentBiz().getCareaContentDAO().findBySql(sql);
		//System.out.println("size========="+list.size());
		request.setAttribute("teacherName", teacher.getTname());
		request.setAttribute("students", list);

		
		
		return mapping.findForward("newjiaoxue");
	}
	/**
	 * 添加个别化教学目标
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addIAiminfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//String month = request.getParameter("imonth");
		String enactmentDate = request.getParameter("enactmentDate");
		//String targetContent = request.getParameter("targetContent");
		String[] students = request.getParameterValues("student");
		
		//System.out.println("month = "+month+" enactmentDate= "+enactmentDate+"  targetContent="+targetContent);
		//String monthName = ""; //月主题
		TIaiminfo minfo = new TIaiminfo();
		minfo.setTeacherId(teacher.getTeacherId());
		//minfo.setMonth(new Integer(month));
		minfo.setShare(0);//默认不共享
		minfo.setEnactmenter(teacher.getTname());
		minfo.setEnactmentDate(enactmentDate);
		minfo.setType(1);//自定义目标
		minfo.setConcount(0);
		minfo.setTargetContent(teacher.getTname()+" 制定的个别化教学目标");
		minfo.setIaiminfoName(teacher.getTname()+" 制定的个别化教学目标");
		Integer id = this.getIaiminfoBiz().saveIaiminfo(minfo);
		
		
		//添加月主题记录表，先获得所有月份的内容 然后有月主题的则添加一条记录后并更新月内容
		//List<TImonthcontent> listcontent = this.getImonthcontentBiz().getTImonthcontent(teacher.getTeacherId(), month, 0, 100);
		this.saveIMonthInfo(teacher.getTeacherId(), id,enactmentDate);
		
			//添加个别学生目标关系记录
		if(null!=students&&students.length>0){
			for(String s:students){
				TIaimtostudent tistudent = new TIaimtostudent();
				tistudent.setIaimId(id);
				tistudent.setStudentId(new Integer(s));
				tistudent.setTeacherId(teacher.getTeacherId());
				this.getIaimToStudentBiz().saveIaimToStudent(tistudent);
			}
		
		}
		//添加完成后保持主题id
		
		request.getSession().setAttribute("iaiminfo", this.getIaiminfoBiz().findById(id));
		return new ActionForward("/beike/imonthinfo.do?method=getIMonthinfo");
	}
	/**
	 * 保存月主题记录
	 * @param TeacherId
	 * @return
	 */
	private boolean saveIMonthInfo(Integer teacherId,Integer iaimId,String enactmentDate){
		for(int month = 1;month<11;month++){
			List<TImonthcontent> listcontent = this.getImonthcontentBiz().getTImonthcontent(teacherId, month+"", 0, 100);
			if(listcontent!=null&&listcontent.size()>0){
				TImonthinfo monthinfo = new TImonthinfo();
				monthinfo.setIaimId(iaimId);
				monthinfo.setTeacherId(teacherId);
				monthinfo.setMonthType(month);
				monthinfo.setMonthName(listcontent.get(0).getMonthThemeName());//设置月主题名称
				monthinfo.setEnactmentDate(enactmentDate);
				Integer monthinfoId = this.getImonthinfoBiz().saveImonthinfo(monthinfo);
				for(TImonthcontent m:listcontent){
					m.setMonthID(monthinfoId);
					m.setIaimId(iaimId);
					this.getImonthcontentBiz().updateImonthcontent(m);
				}
				
			}
		}
		return true;
	}
	/**
	 * 获得月主题教学目标 带分页
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIAiminfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		// 开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		int total = this.getIaiminfoBiz().getIaiminfoTotal(teacher.getTeacherId());
		List list  = this.getIaiminfoBiz().getIaiminfo(teacher.getTeacherId(), (index-1)*pageSize, pageSize);
		
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		request.setAttribute("iaiminfos", list);
		//System.out.println("list size======="+list.size());
		request.setAttribute("tname",teacher.getTname() );
		return mapping.findForward("gebieyuezhuti");
	}
	/**
	 * 初始化 个别目标编辑页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initIAiminfoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//获得个别目标对应的学生
		String sql = "select ic.IClassID,ic.Name from individualclass ic where ic.TeacherID="+teacher.getTeacherId();
		
		List list = this.getCareaContentBiz().getCareaContentDAO().findBySql(sql);
		request.setAttribute("teacherName", teacher.getTname());
		request.setAttribute("students", list);

		String targetId = request.getParameter("targetId");
		request.setAttribute("targetId", targetId);		
		//return new ActionForward("/jsp/mubiaozhiding/edit_Gebielinyu.jsp");
		return new ActionForward("/jsp/mubiaozhiding/edit_newjiaoxue.jsp");
	}
	/**
	 * 编辑目标时 直接跳转
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editIAiminfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		return new ActionForward("/beike/imonthinfo.do?method=getIMonthinfo");
	}
	
}
